package ystp.service;

import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ystp.dao.XinguotanjieguoMapper;
import ystp.dao.YuecaikangjunyaowujieguoMapper;
import ystp.model.*;
import ystp.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service
public class XinguotanjieguoServcie {

    @Resource
    XinguotanjieguoMapper ddMapper;

    @Resource
    XinguotanService ddXinguotanService;

    @Resource
    YsuserService ddYsuserService;

//    public List<Map<String,Object>> findByxiangmuId(String mmXiangmuId){
//        return ddMapper.findByxiangmuId(mmXiangmuId);
//    }

    public Xinguotanjieguo findOne(String ppId){
        return ddMapper.selectByPrimaryKey(ppId);
    }

    public Integer getXinguotanResultCount(String mmxiangmuId,String mmuserId,String mmresult){
        Object mmCount = ddMapper.getXinguotanResultCount(mmxiangmuId,mmuserId,mmresult);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean add(Xinguotanjieguo ppXinguotanjieguo){
        return ddMapper.insert(ppXinguotanjieguo)==1;
    }
    public boolean modify(Xinguotanjieguo ppXinguotanjieguo){
        return ddMapper.updateByPrimaryKeySelective(ppXinguotanjieguo)==1;
    }

    public Xinguotanjieguo checkExistByUserId(String ppId,String ppUserId){
        XinguotanjieguoExample mmExample = new XinguotanjieguoExample();
        mmExample.createCriteria().andDetailidEqualTo(ppId).andUseridEqualTo(ppUserId);
        return ddMapper.selectByExample(mmExample).size()>0?ddMapper.selectByExample(mmExample).get(0):null;
    }

    public int countByDetailId(String ppDetailId){
        XinguotanjieguoExample mmExample = new XinguotanjieguoExample();
        mmExample.createCriteria().andDetailidEqualTo(ppDetailId).andResultEqualTo("1");
        return ddMapper.countByExample(mmExample);
    }


    public void daochuqianzibiao(@RequestParam("xiangmuid") String ppxiangmuid,
                                 @RequestParam("userid") String ppuserid,
                                 HttpServletRequest request, HttpServletResponse response){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddXinguotanService.findXinguotanBiaojueByXiangmuId(ppxiangmuid,ppuserid,"","","",1,999);
        Ysuser mmuser=ddYsuserService.findOne(ppuserid);
        String mmusername=mmuser.getUsername();
        OutputStream os=null;
        WritableWorkbook wbook=null;
        try
        {
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String wenjiantitle = "国家谈判药品"+mmusername+"委员结果表"+ StringHandle.tiemString(); //文件名
            String tmptitle = "东部战区总医院国家谈判药品目录投票表（"+mmusername+"）"; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            SheetSettings settings=wsheet.getSettings();
            settings.setProtected(true);
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);

            WritableCellFormat wcfFC3 = new WritableCellFormat(wfont);
            wcfFC3.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC3.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC3);

            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC3));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,600); //第一行高度
            wsheet.setRowView(1,600); //第二行高度
            wsheet.mergeCells(0, 0, 10, 0);  //合并单元格(第一列的第一行和第十三列的第一行合并)
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,20); //宽度
            wsheet.setColumnView(2,5); //医保宽度
            wsheet.setColumnView(3,5); //宽度
            wsheet.setColumnView(4,5); //宽度
            wsheet.setColumnView(5,11); //宽度
            wsheet.setColumnView(6,11); //宽度
            wsheet.setColumnView(7,40); //宽度
            wsheet.setColumnView(8,10); //宽度
            wsheet.setColumnView(9,10); //宽度
            wsheet.setColumnView(10,10); //宽度

            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            // 不需要边框和居中的样式
            WritableCellFormat wcfFC2 = new WritableCellFormat(wfont);
            wcfFC2 = new WritableCellFormat(wfont);
            wcfFC2.setWrap(true);
            //  在  1 列  2 行 写入内容
            wsheet.addCell(new Label(0, 1, "投票人(签字):投票内容已核对无误。"+mmusername,wcfFC2));
            // 把  0 列 2 行 到 9 列 2 行合并
            wsheet.mergeCells(0, 1, 7, 1);


            wsheet.addCell(new Label(8, 1, "投票日期："+StringHandle.shorttiemString(),wcfFC2));
            wsheet.mergeCells(8, 1, 10, 1);


            wsheet.addCell(new Label(0, 2, "序号",wcfFC));
            wsheet.addCell(new Label(1, 2, "药品名称",wcfFC));
            wsheet.addCell(new Label(2, 2, "医保",wcfFC));
            wsheet.addCell(new Label(3, 2, "院本部药",wcfFC));
            wsheet.addCell(new Label(4, 2, "秦淮药",wcfFC));
            wsheet.addCell(new Label(5, 2, "专科分组",wcfFC));
            wsheet.addCell(new Label(6, 2, "申请科室",wcfFC));
            wsheet.addCell(new Label(7, 2, "国家平台备注信息",wcfFC));
            wsheet.addCell(new Label(8, 2, "规格1",wcfFC));
            wsheet.addCell(new Label(9, 2, "规格2",wcfFC));
            wsheet.addCell(new Label(10, 2, "表决意见",wcfFC));

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmKaishihang=3;

            for (int i = 0; i < mmList.size(); i++) {
                Map<String,Object> mmMap = mmList.get(i);
                if (mmMap!=null){
                    String mmXuhao = mmMap.get("ordernumber")==null ? "" :mmMap.get("ordernumber").toString();
                    String mmyaopinmingcheng=mmMap.get("yaopinmingcheng")==null ? "" :mmMap.get("yaopinmingcheng").toString();
                    String mmYibao=mmMap.get("yibao")==null ? "" :mmMap.get("yibao").toString();
                    String mmYuanbenbu=mmMap.get("yuanbenbuyao")==null ? "" :mmMap.get("yuanbenbuyao").toString();
                    String mmQinhuai=mmMap.get("qinhuaiyao")==null ? "" :mmMap.get("qinhuaiyao").toString();
                    String mmZhuankefenzu=mmMap.get("zhuankefenzu")==null ? "" :mmMap.get("zhuankefenzu").toString();
                    String mmShenqingkeshi=mmMap.get("shenqingkeshi")==null ? "" :mmMap.get("shenqingkeshi").toString();
                    String mmBeizhu=mmMap.get("beizhu")==null ? "" :mmMap.get("beizhu").toString();
                    String mmBeizhu2=mmMap.get("beizhu2")==null ? "" :mmMap.get("beizhu2").toString();
                    String mmBeizhu3=mmMap.get("beizhu3")==null ? "" :mmMap.get("beizhu3").toString();
                    String mmresult=mmMap.get("result")==null ? "" :mmMap.get("result").toString();
                    String mmbiaojueren=mmMap.get("username")==null ? "" :mmMap.get("username").toString();

                    String mmyijian="";
                    if("1".equals(mmresult)){
                        mmyijian="同意";
                    }else if("0".equals(mmresult)){
                        mmyijian="不同意";
                    }else {
                        mmyijian="不同意";
                    }
                    wsheet.addCell(new Label(0, mmKaishihang,mmXuhao,wcfFC));
                    wsheet.addCell(new Label(1, mmKaishihang,mmyaopinmingcheng,wcfFC));
                    wsheet.addCell(new Label(2, mmKaishihang,mmYibao,wcfFC));
                    wsheet.addCell(new Label(3, mmKaishihang,mmYuanbenbu,wcfFC));
                    wsheet.addCell(new Label(4, mmKaishihang,mmQinhuai,wcfFC));
                    wsheet.addCell(new Label(5, mmKaishihang,mmZhuankefenzu,wcfFC));
                    wsheet.addCell(new Label(6, mmKaishihang,mmShenqingkeshi,wcfFC));
                    wsheet.addCell(new Label(7, mmKaishihang,mmBeizhu,wcfFC));
                    wsheet.addCell(new Label(8, mmKaishihang,mmBeizhu2,wcfFC));
                    wsheet.addCell(new Label(9, mmKaishihang,mmBeizhu3,wcfFC));
                    wsheet.addCell(new Label(10, mmKaishihang,mmyijian,wcfFC));
                    mmKaishihang = mmKaishihang+1;
                }
            }
            wbook.write(); // 写入文件
            wbook.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public List<Map<String,Object>>getHuizongbiao(String ppXiangmuId){
        return ddMapper.getHuizongbiao(ppXiangmuId);
    }
}
